import 'package:efood_multivendor/controller/order_controller.dart';
import 'package:efood_multivendor/data/model/response/order_model.dart';
import 'package:efood_multivendor/util/dimensions.dart';
import 'package:efood_multivendor/view/base/no_data_screen.dart';
import 'package:efood_multivendor/view/screens/order/widget/order_list_widget.dart';
import 'package:efood_multivendor/view/screens/order/widget/order_shimmer.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class OrderView extends StatelessWidget {
  final bool isRunning;
  OrderView({required this.isRunning});

  @override
  Widget build(BuildContext context) {
    final ScrollController scrollController = ScrollController();

    return Scaffold(
      backgroundColor: Colors.white,
      body: GetBuilder<OrderController>(builder: (orderController) {
        List<OrderModel>? orderList;
        bool paginate = false;
        int pageSize = 1;
        int offset = 1;
        if (orderController.runningOrderList != null &&
            orderController.historyOrderList != null) {
          orderList = isRunning
              ? orderController.runningOrderList
              : orderController.historyOrderList;
          paginate = isRunning
              ? orderController.runningPaginate
              : orderController.historyPaginate;
          pageSize = isRunning
              ? (orderController.runningPageSize! / 10).ceil()
              : (orderController.historyPageSize! / 10).ceil();
          offset = isRunning
              ? orderController.runningOffset
              : orderController.historyOffset;
        }
        scrollController.addListener(() {
          if (scrollController.position.pixels ==
                  scrollController.position.maxScrollExtent &&
              orderList != null &&
              !paginate) {
            if (offset < pageSize) {
              Get.find<OrderController>().setOffset(offset + 1, isRunning);
              Get.find<OrderController>().showBottomLoader(isRunning);
              if (isRunning) {
                Get.find<OrderController>().getRunningOrders(offset + 1);
              } else {
                Get.find<OrderController>().getHistoryOrders(offset + 1);
              }
            }
          }
        });

        return orderList != null
            ? orderList.length > 0
                ? RefreshIndicator(
                    onRefresh: () async {
                      if (isRunning) {
                        await orderController.getRunningOrders(1);
                      } else {
                        await orderController.getHistoryOrders(1);
                      }
                    },
                    child: Scrollbar(
                        child: SingleChildScrollView(
                      controller: scrollController,
                      physics: AlwaysScrollableScrollPhysics(),
                      child: Center(
                          child: SizedBox(
                        width: Dimensions.WEB_MAX_WIDTH,
                        child: OrderListWidget(
                            orderList: orderList,
                            paginate: paginate,
                            orderController: orderController),
                      )),
                    )),
                  )
                : NoDataScreen(text: 'no_order_found'.tr)
            : OrderShimmer(orderController: orderController);
      }),
    );
  }
}
